
package com.jf.cloud.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.product.vo.GiveawaySpuVO;
import com.jf.cloud.common.product.vo.GiveawayVO;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.product.dto.GiveawaySpuDTO;
import com.jf.cloud.product.mapper.GiveawaySpuMapper;
import com.jf.cloud.product.model.GiveawaySpu;
import com.jf.cloud.product.service.GiveawaySpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 赠品套装商品项
 *
 * @author zz
 * @date 2022-08-16 11:11:59
 */
@Service
public class GiveawaySpuServiceImpl implements GiveawaySpuService {

    @Autowired
    private GiveawaySpuMapper giveawaySpuMapper;

    @Override
    public PageVO<GiveawaySpuVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> giveawaySpuMapper.list());
    }

    @Override
    public List<GiveawaySpuVO> getBySpuId(Long spuId) {
        return giveawaySpuMapper.getBySpuId(spuId);
    }

    @Override
    public PageVO<SpuVO> getMainSpuPage(PageDTO pageDTO, GiveawaySpuDTO giveawaySpuDTO) {
        return PageUtil.doPage(pageDTO, () -> giveawaySpuMapper.getMainSpuPage(giveawaySpuDTO, I18nMessage.getLang()));
    }

    @Override
    public List<GiveawaySpuVO> getByGiveawayId(Long giveawayId) {
        return giveawaySpuMapper.getByGiveawayId(giveawayId, I18nMessage.getLang(), StatusEnum.DELETE.value());
    }

    @Override
    @Cacheable(cacheNames = "getGiveawayBySpuId", key = "#spuId")
    public GiveawayVO getGiveawayBySpuId(Long spuId) {
        return giveawaySpuMapper.getGiveawayBySpuId(spuId);
    }

    @Override
    public void save(GiveawaySpu giveawaySpu) {
        giveawaySpuMapper.save(giveawaySpu);
    }

    @Override
    public boolean updateStatus(Long giveawayId, Integer status) {
        return giveawaySpuMapper.updateStatus(giveawayId, status);
    }

    @Override
    public void deleteById(Long giveawaySpuId) {
        giveawaySpuMapper.deleteById(giveawaySpuId);
    }

    @Override
    public void insertBatch(List<GiveawaySpuDTO> giveawaySpuList) {
        if (CollUtil.isEmpty(giveawaySpuList)) {
            return;
        }
        giveawaySpuList.forEach(s->s.setStatus(1));
        giveawaySpuMapper.insertBatch(giveawaySpuList);
    }

    @Override
    public void batchDeleteByIds(List<Long> giveawaySpuIds) {
        if (CollUtil.isEmpty(giveawaySpuIds)) {
            return;
        }
        giveawaySpuMapper.batchDeleteByIds(giveawaySpuIds);
    }

    @Override
    public void changeGiveawayActivityStatusByGiveawayIdList(List<Long> giveawayIdList) {
        giveawaySpuMapper.changeGiveawayActivityStatusByGiveawayIdList(giveawayIdList);
    }


    @Override
    @CacheEvict(cacheNames = "getGiveawayBySpuId", key = "#spuId")
    public void removeGiveawayCacheBySupId(Long spuId) {
    }

    @Override
    public void removeGiveawayCacheBatch(List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        List<String> key = new ArrayList<>();
        for (Long spuId : spuIds) {
            //removeGiveawayCacheBySupId(spuId);
            key.add("getGiveawayBySpuId" + CacheNames.UNION + spuId);
        }
        RedisUtil.del(key);
    }

}
